package gui.domain;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;

public class MoveableLens extends MoveableComponent {
	private Image img;
	private double zoom = 0;
	private double halfWith;
	private double halfHeight;
	private int BREDDE = 200;
	private int HOYDE = 200;
	private int backgroundBeginX;
	private int backgroundBeginY;
	private int backgroundEndX;
	private int backgroundEndY;
	private int centerX;
	private double centerY;

	public MoveableLens(Background b) {
		img = b.getImage();
		setSize(BREDDE, HOYDE);
		centerX = 100;
		centerY = 100;
	}

	public void paint(Graphics g) {
		calculateZoomRectangle();
		g.drawRect(1, 1, getWidth()-2, getHeight()-2);
		g.drawImage(img, 0, 0, BREDDE, HOYDE, backgroundBeginX, backgroundBeginY, backgroundEndX, backgroundEndY, null);
	}
	
	private void calculateZoomRectangle() {
		  backgroundBeginX = (int) Math.round(centerX - halfWith * zoom);
		  backgroundBeginY = (int) Math.round(centerY - halfHeight * zoom);
		  backgroundEndX = (int) Math.round(centerX + halfWith * zoom);
		  backgroundEndY = (int) Math.round(centerY + halfHeight * zoom);
		 }
	
	 public void mouseDragged(MouseEvent source) {
		  super.mouseDragged(source);
		  centerX = getX() + BREDDE / 2;
		  centerY = getY() + HOYDE / 2;
		  //System.out.println(centerX);
		  //System.out.println(centerY);
	 }
		 
	public void setZoom(int value) {
		zoom = 1 + ((double)value / 100);
		halfWith = (BREDDE * zoom  / 2 );
		halfHeight = (HOYDE * zoom / 2 );
		repaint();
	}

}
